package com.payment.controller;

import com.alipay.v3.ApiException;
import com.common.domain.pojo.Result;
import com.payment.domain.dto.OrderPaymentDtoRefundDto;
import com.payment.domain.dto.OrderPaymentDto;
import com.payment.service.IPaymentRecordService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * <p>
 * 支付记录表 前端控制器
 * </p>
 *
 * @author 小杰大人
 * @since 2025-09-29
 */
@RestController
@RequestMapping("/api/payment")
public class PaymentRecordController {

    @Resource
    IPaymentRecordService iPaymentRecordService;

    // 调起支付界面
    @PostMapping
    public Result<String> initiatePayment(@RequestBody OrderPaymentDto orderPaymentDto) {
        return Result.success(iPaymentRecordService.initiatePayment(orderPaymentDto));
    }

    // 支付结果异步回调
    /*
    https: //商家网站通知地址?voucher_detail_list=[{"amount":"0.20","merchantContribute":"0.00","name":"5折券","otherContribute":"0.20","type":"ALIPAY_DISCOUNT_VOUCHER","voucherId":"2016101200073002586200003BQ4"}]&fund_bill_list=[{"amount":"0.80","fundChannel":"ALIPAYACCOUNT"},{"amount":"0.20","fundChannel":"MDISCOUNT"}]&subject=PC网站支付交易&trade_no=2016101221001004580200203978&gmt_create=2016-10-12 21:36:12&notify_type=trade_status_sync&total_amount=1.00&out_trade_no=mobile_rdm862016-10-12213600&invoice_amount=0.80&seller_id=2088201909970555&notify_time=2016-10-12 21:41:23&trade_status=TRADE_SUCCESS&gmt_payment=2016-10-12 21:37:19&receipt_amount=0.80&passback_params=passback_params123&buyer_id=2088102114562585&buyer_open_id=074a1CcTG1LelxKe4xQC0zgNdId0nxi95b5lsNpazWYoCo5&app_id=2016092101248425&notify_id=7676a2e1e4e737cff30015c4b7b55e3kh6& sign_type=RSA2&buyer_pay_amount=0.80&sign=***&point_amount=0.00
     */
    @PostMapping("/callback")
    public void paymentCallback(HttpServletRequest request, HttpServletResponse response) throws IOException, ApiException {
        iPaymentRecordService.paymentCallback(request, response);
    }

    // 查询支付结果
    @GetMapping("/query")
    public Result<Boolean> queryPaymentResult(@RequestParam("orderNo") String orderNo) {
        return Result.success(iPaymentRecordService.queryPaymentResult(orderNo));
    }

    // 取消订单
    @PostMapping("/cancel")
    public Result<Boolean> cancel(@RequestParam("orderNo") String orderNo) {
        return Result.success(iPaymentRecordService.cancel(orderNo));
    }

    // 支付成功
    @PostMapping("/success")
    public Result<Boolean> paymentSuccess(@RequestParam("orderNo") String orderNo) {
        return Result.success(iPaymentRecordService.paymentSuccess(orderNo));
    }


}
